//
//  CallTelphoneViewController.m
//  CallTelphone
//
//  Created by mac on 2023/5/22.
//

#import "CallTelphoneViewController.h"
#import "CallingViewController.h"
#import "CallinViewController.h"

@interface CallTelphoneViewController ()
@property (weak, nonatomic) IBOutlet UIButton *addPhone;
@property (weak, nonatomic) IBOutlet UITextView *telphoneNumberTextView;
@property (weak, nonatomic) IBOutlet UIButton *btn1;
@property (weak, nonatomic) IBOutlet UIButton *btn2;
@property (weak, nonatomic) IBOutlet UIButton *btn3;
@property (weak, nonatomic) IBOutlet UIButton *btn4;
@property (weak, nonatomic) IBOutlet UIButton *btn5;
@property (weak, nonatomic) IBOutlet UIButton *btn6;
@property (weak, nonatomic) IBOutlet UIButton *btn7;
@property (weak, nonatomic) IBOutlet UIButton *btn8;
@property (weak, nonatomic) IBOutlet UIButton *btn9;
@property (weak, nonatomic) IBOutlet UIButton *btn10;
@property (weak, nonatomic) IBOutlet UIButton *btn11;
@property (weak, nonatomic) IBOutlet UIButton *btn12;
@property (weak, nonatomic) IBOutlet UIButton *btn13;
@property (weak, nonatomic) IBOutlet UIButton *deleteButton;
@property (nonatomic,strong) CallinViewController *callinVC;
@end

@implementation CallTelphoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(Incoming:) name:@"Incoming" object:nil];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}


- (IBAction)btn1:(id)sender {
    [self isViewDeleteButton:@"1"];
}

- (IBAction)btn2:(id)sender {
    [self isViewDeleteButton:@"2"];
}

- (IBAction)btn3:(id)sender {
    [self isViewDeleteButton:@"3"];
}

- (IBAction)btn4:(id)sender {
    [self isViewDeleteButton:@"4"];
}

- (IBAction)btn5:(id)sender {
    [self isViewDeleteButton:@"5"];
}

- (IBAction)btn6:(id)sender {
    [self isViewDeleteButton:@"6"];
}

- (IBAction)btn7:(id)sender {
    [self isViewDeleteButton:@"7"];
}

- (IBAction)btn8:(id)sender {
    [self isViewDeleteButton:@"8"];
}

- (IBAction)btn9:(id)sender {
    [self isViewDeleteButton:@"9"];
}

- (IBAction)btn10:(id)sender {
    [self isViewDeleteButton:@"*"];
}

- (IBAction)btn11:(id)sender {
    [self isViewDeleteButton:@"0"];
}

- (IBAction)btn12:(id)sender {
    [self isViewDeleteButton:@"#"];
}
- (IBAction)deleteTel:(id)sender {
    NSString *txt = self.telphoneNumberTextView.text;
    int count = (txt.length == 4 || txt.length == 9) ? 2 : 1;
    NSString *newText = [txt substringToIndex:[txt length]-count];
    self.telphoneNumberTextView.text = newText;
    self.deleteButton.hidden = self.telphoneNumberTextView.text.length == 0;
    self.addPhone.hidden = self.telphoneNumberTextView.text.length == 0;
}

- (void)isViewDeleteButton:(NSString *)add{
    NSString *txt = self.telphoneNumberTextView.text;
    NSString *addText = [NSString stringWithFormat:@"%@",add];
    if (txt.length == 3 || txt.length == 8){
        addText = [NSString stringWithFormat:@" %@",add];
    }
    self.telphoneNumberTextView.text = [txt stringByAppendingString:addText];
    self.deleteButton.hidden = self.telphoneNumberTextView.text.length == 0;
    self.addPhone.hidden = self.telphoneNumberTextView.text.length == 0;
}

- (IBAction)btn13:(id)sender {
    if(self.telphoneNumberTextView.text.length > 0){
        CallingViewController *callingVC = [[CallingViewController alloc] initWithNibName:@"CallingViewController" bundle:nil];
        callingVC.telphoneNumber = self.telphoneNumberTextView.text;
        callingVC.modalPresentationStyle = UIModalPresentationOverFullScreen;
        [self presentViewController:callingVC animated:NO completion:^{
            self.telphoneNumberTextView.text = @"";
        }];
    }
}


- (void)Incoming:(NSNotification *)notificaiton{
    if (notificaiton.userInfo) {
        NSString *phone = notificaiton.userInfo[@"aps"][@"alert"][@"body"];
        if (phone.length > 0){
            if ([phone isEqualToString:@"接"]){
                if (_callinVC){
                    [_callinVC inComingUp:nil];
                }
            }else if ([phone isEqualToString:@"挂"]){
                if (_callinVC){
                    [_callinVC inComingDown:nil];
                }
            }else{
                if (!RE_SINGLETON(CallTelphoneManager).isCalling) {
                    NSMutableString *_phone = [NSMutableString stringWithString:phone];
                    [_phone insertString:@" " atIndex:3];
                    [_phone insertString:@" " atIndex:8];
                    self.addPhone.hidden = YES;
                    RE_SINGLETON(CallTelphoneManager).isCalling = YES;
                    self.callinVC = [[CallinViewController alloc] initWithNibName:@"CallinViewController" bundle:nil];
                    _callinVC.telphoneNumber = _phone;
                    _callinVC.modalPresentationStyle = UIModalPresentationFullScreen;
                    [self presentViewController:_callinVC animated:NO completion:^{
                        
                    }];
                }
            }
        }
    }
}


@end
